package com.redsoil.house.ui.property.presenter;

import com.redsoil.house.app.AppContext;
import com.redsoil.house.base.RxPresenter;
import com.redsoil.house.model.DataManager;
import com.redsoil.house.model.bean.OnePropertyVO;
import com.redsoil.house.model.bean.PropertyLeaseVO;
import com.redsoil.house.model.bean.RoomEarnVO;
import com.redsoil.house.model.http.exception.ApiException;
import com.redsoil.house.model.http.response.HttpResponse;
import com.redsoil.house.ui.property.contract.PropertyIncomeContract;
import com.redsoil.house.utils.RxUtil;
import com.redsoil.house.widget.CommonSubscriber;

import javax.inject.Inject;

/**
 * 作者：weidingqiang on 2017/9/7 15:39
 * 邮箱：dqwei@iflytek.com
 */

public class PropertyIncomePresenter extends RxPresenter<PropertyIncomeContract.View> implements PropertyIncomeContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public PropertyIncomePresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void attachView(PropertyIncomeContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    private void registerEvent(){

    }

    @Override
    public void getRoomEarnList(String roomId, String accountDate ,String moneyType,String realtyId) {
        addSubscribe(mDataManager.getRoomEarnList(AppContext.getInstance().getToken(),roomId,accountDate,moneyType,realtyId)
                .compose(RxUtil.<HttpResponse<RoomEarnVO>>rxSchedulerHelper())
                .compose(RxUtil.<RoomEarnVO>handleTestResult())
                .subscribeWith(
                        new CommonSubscriber<RoomEarnVO>(mView) {
                            @Override
                            public void onNext(RoomEarnVO data) {

                                mView.getRoomEarnListSuccess(data);
                            }

                            @Override
                            public void onError(Throwable e) {
                                //当数据返回为null时 做特殊处理
                                try {
                                    int code = ((ApiException) e).getCode();
                                    mView.responeError(e.getMessage());
                                    return;
                                } catch (Exception ex) {

                                }
                                mView.responeError("数据请求失败，请检查网络！");
                            }

                        }
                )
        );
    }

    @Override
    public void getOneProperty(String id) {
        addSubscribe(mDataManager.getOneProperty(AppContext.getInstance().getToken(),id,AppContext.getInstance().getCurrentMoneyType(),"2")
                .compose(RxUtil.<HttpResponse<OnePropertyVO>>rxSchedulerHelper())
                .compose(RxUtil.<OnePropertyVO>handleTestResult())
                .subscribeWith(
                        new CommonSubscriber<OnePropertyVO>(mView) {
                            @Override
                            public void onNext(OnePropertyVO data) {

                                mView.getOnePropertySuccess(data);
                            }

                            @Override
                            public void onError(Throwable e) {
                                //当数据返回为null时 做特殊处理
                                try {
                                    int code = ((ApiException) e).getCode();
                                    mView.responeError(e.getMessage());
                                    return;
                                } catch (Exception ex) {

                                }
                                mView.responeError("数据请求失败，请检查网络！");
                            }

                        }
                )
        );
    }
}
